package com.denlaku.longan.controller.impl;

import com.denlaku.longan.anno.VarBy;
import com.denlaku.longan.controller.CustomApiController;
import com.denlaku.longan.http.Resp;
import com.denlaku.longan.http.Return;
import com.denlaku.longan.qo.CustomApiQuery;
import com.denlaku.longan.qo.Query;
import com.denlaku.longan.service.CustomApiService;
import com.denlaku.longan.vo.CustomApi;
import com.denlaku.longan.vo.PageBy;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author tianx
 */
@RestController
public class CustomApiControllerImpl implements CustomApiController {
    @Autowired
    private CustomApiService customApiService;

    @Override
    public Return<Long> add(@RequestBody CustomApi customApi) {
        customApiService.add(customApi);
        return Resp.success(customApi.getId());
    }

    @Override
    public Return<Void> update(@RequestBody CustomApi customApi) {
        if (customApi.getId() == null) {
            return Resp.success();
        }
        customApiService.update(customApi);
        return Resp.success();
    }

    @Override
    public Return<Void> delete(Long id) {
        customApiService.delete(Query.of(id));
        return Resp.success();
    }

    @Override
    public Return<CustomApi> get(Long id) {
        CustomApi customApi = customApiService.get(Query.of(id));
        return Resp.success(customApi);
    }

    @VarBy
    @Override
    public Return<List<CustomApi>> pageList(CustomApiQuery query, PageBy pageBy) {
        PageHelper.startPage(pageBy.getPage(), pageBy.getSize());
        List<CustomApi> list = customApiService.list(query);
        return Resp.success(list);
    }

}
